var core = require('core');
var xml2js = require('xml2js');

var node = function(req, res, next){
    var xml = "";
    req.on('data', function(data){
        xml += data;
    });
    req.on('end', function(){
        var xmlParser = new xml2js.Parser({explicitArray : false, ignoreAttrs : true});
        xmlParser.parseString(xml, function(err, json){

            var oid = json.xml.attach;
            var transactionId = json.xml.transaction_id;
            var params = {
                query : JSON.stringify({
                    oid : oid
                }),
                update : JSON.stringify({
                    status : true,
                    transaction_id : transactionId,
                    pay_time : new Date()
                })
            };
            core.api("order/update", params, function(err, doc){
                return res.send("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
            });
        });
    });
};
module.exports = node;
